package iscript.uiFramework.components.button 
{
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import iscript.uiFramework.components.text.Label;
	import iscript.uiFramework.effect.Effect;
	import iscript.uiFramework.event.UIComponentEvent;
	import iscript.uiFramework.UIFramework;
	import iscript.uiFramework.util.layout.LayoutManager;
	import iscript.uiFramework.util.Scale9GridBitmap;

	
	[Event(name="buttonevent_click",type="iscript.uiFramework.event.ButtonEvent")]
	
	public class BaseButton  extends AbstractButton 
	{
        
		protected var _normal_bitmapdata:BitmapData;
		protected var _down_bitmapdata:BitmapData;
		protected var _hover_bitmapdata:BitmapData;
		protected var _disable_bitmapdata:BitmapData;
		private var _textLabel:Label;
		protected var _buttonScale9Grid:Scale9GridBitmap;

		
		
		public function BaseButton(preferWidth:Number=50,preferHeight:Number=23) 
		{
			super(preferWidth,preferHeight);
		} 		
		
		override public function set theme(value:String):void 
		{
			if(this.theme != value){
			  super.theme = value;
			  this._disable_bitmapdata  =  UIFramework.resource.getButtonBitmapData(theme,ButtonState.DISABLED);
		      this._down_bitmapdata     =  UIFramework.resource.getButtonBitmapData(theme,ButtonState.DOWN);
		      this._hover_bitmapdata    =  UIFramework.resource.getButtonBitmapData(theme,ButtonState.HOVER);
		      this._normal_bitmapdata   =  UIFramework.resource.getButtonBitmapData(theme, ButtonState.NORMAL);
			  if (_flat)
			  this._normal_bitmapdata = new BitmapData(this._normal_bitmapdata.width, this._normal_bitmapdata.height, true, 0xFFFFFF);
			  state = this._state;
			}
		}
		
		override protected function initialize():void 
		{
			 this._disable_bitmapdata  =  UIFramework.resource.getButtonBitmapData(theme,ButtonState.DISABLED);
		     this._down_bitmapdata     =  UIFramework.resource.getButtonBitmapData(theme,ButtonState.DOWN);
		     this._hover_bitmapdata    =  UIFramework.resource.getButtonBitmapData(theme,ButtonState.HOVER);
		     this._normal_bitmapdata   =  UIFramework.resource.getButtonBitmapData(theme, ButtonState.NORMAL);
			 
			 _buttonScale9Grid = new Scale9GridBitmap(_normal_bitmapdata, viewRectangle);
			 _buttonScale9Grid.width = _preferWidth;
			 _buttonScale9Grid.height = _preferHeight;
			 addChild(_buttonScale9Grid);
			 
			 _textLabel = new Label();
			 _textLabel.text = "";
			 _textLabel.addEventListener(UIComponentEvent.UPDATEVIEW,buttonLabelUpdateRender)
			 _textLabel.render(this);
			 
			 super.initialize();
		}
		
		override public function normal():void 
		{
			this.state = ButtonState.NORMAL;
			toggle = _toggle;
			super.normal();
		}
		
		
		
		protected function get  viewRectangle():Rectangle {			
			return  UIFramework.resource.getElementRectangle("Button", this.theme)
		}
		
		private function buttonLabelUpdateRender(e:Event):void 
		{
			if (_textLabel.width >= this.width) {
				this.width = _textLabel.width+10 ;
				}else {
					if (this.width - _textLabel.width <10 ) {
			        	this.width = _textLabel.width+10 ;
				      }
					}
			
			if (_textLabel.height >= this.height) {
				this.height = _textLabel.height ;
				}else {
					if (this.height - _textLabel.height <6 ) {
			        	this.height = _textLabel.height+4 ;
				      }
				}
			setAlignLabel(_align);
		}
		
		private var _align:String= LayoutManager.CC;
	    public function setAlignLabel(value:String):void {
			   _align = value;
			   layoutManager.setAlign(_textLabel, value);
			   _textLabel.y -= 1;
			}
 
			 
		override public function get x():Number 
		{
			return super.x;
		}
		
		override public function get y():Number 
		{
			return super.y;
		}
				
		override public function set width(value:Number):void 
		{
			 _buttonScale9Grid.width = value;
			 setAlignLabel(_align);
			 updateRender();
			 super.width = value;
		}
		
	 	override public function get width():Number
		{
			return _buttonScale9Grid.width;
		} 
		
		override public function set height(value:Number):void 
		{
			 _buttonScale9Grid.height = value;
			 setAlignLabel(_align);
			 updateRender();
			 super.height = value;
		}
		
		override public function get height():Number 
		{
			return _buttonScale9Grid.height
		}
		
		override public function get enabled():Boolean 
		{
			return super.enabled;
		}
		
		override public function set enabled(value:Boolean):void 
		{
			super.enabled = value;
			this.buttonMode = value;
			this.useHandCursor = value;
		}
		
		override public function get disabled():Boolean 
		{
			return super.disabled;
		}
		
		override public function set disabled(value:Boolean):void 
		{
			super.disabled = value;
			if (value) { 
				_buttonScale9Grid.updateState(this._disable_bitmapdata); 
				_state = ButtonState.DISABLED;
				_textLabel.disabled = value;
				Effect.gray(this);
				}else{
				_buttonScale9Grid.updateState(this._normal_bitmapdata);
				_state = ButtonState.NORMAL;
				_textLabel.disabled = value;
				Effect.reset(this);
				}			
		}
		
		public function get textLabel():Label 
		{
			return _textLabel;
		}
		
		override public function get state():String 
		{
			return _state;
		}
		
		/**
		 *  class ButtonState
		 */
		override public function set state(value:String):void 
		{
			_state = value;
			switch(value) {
				   case ButtonState.NORMAL:
				   _buttonScale9Grid.updateState (this._normal_bitmapdata, viewRectangle);
				   IsClickOver = false;
				   break;
				   case ButtonState.HOVER:
				   _buttonScale9Grid.updateState(this._hover_bitmapdata,viewRectangle);
				   break;
				   case ButtonState.DOWN:
				   _buttonScale9Grid.updateState(this._down_bitmapdata,viewRectangle);
				   break;
				   case ButtonState.DISABLED:
				   _buttonScale9Grid.updateState(this._disable_bitmapdata,viewRectangle);
				   break;				 
				 }
			updateRender();
		}
		
		override public function get text():String 
		{
			return _textLabel.text;
		}
		
		override public function set text(value:String):void 
		{
			_textLabel.text = value;
			setAlignLabel(_align);
		}
		
	  public function get icon():BitmapData 
		{
			return _textLabel.icon;
		}
		
		  public function set icon(value:BitmapData):void 
		{
			_textLabel.icon = value;
			setAlignLabel(_align);
		}	
				

		
		override public function dispose():void 
		{
			if(!Disposed){
			   _textLabel.removeEventListener(UIComponentEvent.UPDATEVIEW, buttonLabelUpdateRender);
			   _textLabel.dispose();
			   super.dispose()
			}
		}
		 
		override protected function onStateClick(e:MouseEvent):void 
		{
			super.onStateClick (e);
			/* if (IsClickOver) {
				   if (_flat) {
					    _buttonScale9Grid.updateState(this._hover_bitmapdata);
					  }
				   
				 }
			 
			if (isActive && _flat) {
				 if (_toggle) {					 
				 _buttonScale9Grid.updateState(this._hover_bitmapdata);
					 }else {
				 _buttonScale9Grid.updateState(this._hover_bitmapdata);
				}
			}*/
				if (isActive &&  _flat && !_toggle) {
				  _buttonScale9Grid.updateState(this._hover_bitmapdata);
				}
				if (isActive &&  _flat && _toggle && IsReleased) {
				  _buttonScale9Grid.updateState(this._hover_bitmapdata);
				}
		}
		
		override protected function onStateMouseDown(e:MouseEvent):void 
		{
			super.onStateMouseDown(e);
			if (isActive && !_toggle){
			   _buttonScale9Grid.updateState(this._down_bitmapdata);
			}
			
			if (isActive && _toggle ) {
				  if (IsReleased) {
					 _buttonScale9Grid.updateState(this._down_bitmapdata);
					 }
			 }
		}	
		
		override protected function onStateMouseUp(e:MouseEvent):void 
		{
			super.onStateMouseUp(e);
			if(hitMouse()){
				if (isActive && !_toggle)
				  _buttonScale9Grid.updateState(this._hover_bitmapdata);
				  
				if(isActive && _toggle ) {
					  if (IsReleased){
					  _buttonScale9Grid.updateState(this._hover_bitmapdata);
					  }
					}
			 }
		}
		
		override protected function onStateRollOut(e:MouseEvent):void 
		{
			super.onStateRollOut(e);	 
			if (isActive && !_toggle)
			_buttonScale9Grid.updateState(this._normal_bitmapdata);
		 	
			if(isActive &&IsClickOver){				
				if (isActive &&  _flat && _toggle && IsReleased ) {
					  _buttonScale9Grid.updateState(this._normal_bitmapdata);
					}
				if (isActive &&  _flat && _toggle && IsPressed ) {
					  _buttonScale9Grid.updateState(this._down_bitmapdata);	
					  }
				if (isActive &&  !_flat && _toggle && IsPressed ) {
					  _buttonScale9Grid.updateState(this._down_bitmapdata);
					  }
				if (isActive &&  !_flat && _toggle && IsReleased ) {
					  _buttonScale9Grid.updateState(this._hover_bitmapdata);
					  }	
			}
			
			if(isActive &&!IsClickOver){
				  _buttonScale9Grid.updateState(this._normal_bitmapdata);

				}
 
		}	
		
		override protected function onStateRollOver(e:MouseEvent):void 
		{
			super.onStateRollOver(e);	
			
			if (isActive && !_toggle)
			_buttonScale9Grid.updateState(this._hover_bitmapdata);	
			
			
			if (isActive && _toggle)
			_buttonScale9Grid.updateState(this._hover_bitmapdata);	
			
			if (isActive && _toggle && _flat)
			_buttonScale9Grid.updateState(this._down_bitmapdata);	
	 
			 
			if (isActive && _toggle && _flat && IsReleased)
			_buttonScale9Grid.updateState(this._hover_bitmapdata);	
		}
		
		override public function set flat(value:Boolean):void 
		{
			if(value){
			   this._normal_bitmapdata = new BitmapData(this._normal_bitmapdata.width, this._normal_bitmapdata.height, true, 0xFFFFFF);
			}else {
			  this._normal_bitmapdata = UIFramework.resource.getButtonBitmapData(theme, ButtonState.NORMAL);	
			}
			_buttonScale9Grid.updateState(this._normal_bitmapdata);	
			_flat = value;
		}
		
		override public function render(container:DisplayObjectContainer = null):void 
		{
			super.render(container);
		}
		
		
		
		
	}

}